package com.dqhl.smartstable.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by mac on 16/10/14.
 * 绑定sn码
 */
public class BindingSNActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_top_left;
    private TextView tv_top_center;
    private EditText et_sn;
    private Button btn_confirm;

    private String strSN;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binding_sn);

        initView();
    }

    private void initView() {
        tv_top_left = (TextView) findViewById(R.id.tv_top_left);
        tv_top_left.setText("返回");
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("绑定SN");
        et_sn = (EditText) findViewById(R.id.et_sn);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_confirm.setEnabled(false);

        tv_top_left.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

        et_sn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                strSN = et_sn.getText().toString().trim();
                if (TextUtils.isEmpty(strSN)) {
                    btn_confirm.setEnabled(false);
                } else {
                    btn_confirm.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_left:
                finish();
                break;
            case R.id.btn_confirm:
                doConfirm();
                break;
        }
    }

    /**
     * 确定
     */
    private void doConfirm() {
        final String strSN = et_sn.getText().toString().trim();
        String token = sp.getString(Constant.TOKEN, "");
        RequestParams params = new RequestParams(Config.Api.bind);
        params.addBodyParameter("case_sign", strSN);
        params.addBodyParameter("token", token);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                if ("2000".equals(msg)) {
                    sp.edit().putString(Constant.SN, strSN).apply();
                    Dlog.e("---caseSN为:" + strSN);
                    forward(MainActivity.class);
                }
                toast(code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
